{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f9e4a826-46b9-45f9-8994-a1712a7f6e99",
   "metadata": {},
   "source": [
    "使用 FastAPI，你可以定义、校验、记录文档并使用任意深度嵌套的模型（归功于Pydantic）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d223ebbf-bc5f-49dd-8e8f-af599fb35bb4",
   "metadata": {},
   "source": [
    "## 嵌套模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cb9af2c-18ef-4a81-b13b-42526222bc61",
   "metadata": {},
   "source": [
    "Pydantic 模型的每个属性都具有类型。\n",
    "\n",
    "但是这个类型本身可以是另一个 Pydantic 模型。\n",
    "\n",
    "因此，你可以声明拥有特定属性名称、类型和校验的深度嵌套的 JSON 对象。\n",
    "\n",
    "上述这些都可以\n",
    "\n",
    "例如，我们可以定义一个 Image 模型\n",
    "\n",
    "然后我们可以将其用作一个属性的类型：任意的嵌套。法。st：\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "37864783-2295-4de9-9ffa-297946d0afa6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [10840]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:58354 - \"POST /items/5 HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [10840]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from fastapi import FastAPI\n",
    "from pydantic import BaseModel\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "class Image(BaseModel):\n",
    "    url: str\n",
    "    name: str\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: str | None = None\n",
    "    price: float\n",
    "    tax: float | None = None\n",
    "    image: Image | None = None\n",
    "\n",
    "\n",
    "@app.post(\"/items/{item_id}\")\n",
    "async def update_item(item_id: int, item: Item):\n",
    "    results = {\"item_id\": item_id, \"item\": item}\n",
    "    return results\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ee85c16-4612-47e4-9c4f-5bb9902cb535",
   "metadata": {},
   "source": [
    "这意味着 FastAPI 将期望类似于以下内容的请求体：\n",
    "{\n",
    "    \"name\": \"Foo\",\n",
    "    \"description\": \"The pretender\",\n",
    "    \"price\": 42.0,\n",
    "    \"tax\": ar\"],\n",
    "    \"image\": {\n",
    "        \"url\": \"http://example.com/baz.jpg\",\n",
    "        \"name\": \"The Foo live\"\n",
    "\n",
    "仅仅进行这样的声明，你将通过 FastAPI 获得：\n",
    "\n",
    "对被嵌入的模型也适用的编辑器支持（自动补全等）\n",
    "数据转换\n",
    "数据校验\n",
    "自动生成文档\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4df05494-da0c-4430-b597-8809fb48a59a",
   "metadata": {},
   "source": [
    "## requests库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5ebdfb8-c1c3-4d57-ba77-b4c409163b8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "url = 'http://127.0.0.1:8009/items/5' \n",
    "data = {\n",
    "    \"name\": \"Foo\",\n",
    "    \"description\": \"The pretender\",\n",
    "    \"price\": 42.0,\n",
    "    \"tax\": 3.2,\n",
    "    \"image\": {\n",
    "        \"url\": \"http://example.com/baz.jpg\",\n",
    "        \"name\": \"The Foo live\"\n",
    "    }\n",
    "}\n",
    "res = requests.post(url, json=data) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 \n",
    "# '{\"item_id\":5,\"item\":{\"name\":\"Foo\",\"description\":\"The pretender\",\"price\":42.0,\"tax\":3.2,\"image\":{\"url\":\"http://example.com/baz.jpg\",\"name\":\"The Foo live\"}}}'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f91889f-469c-4e90-98db-b319029efb7d",
   "metadata": {},
   "source": [
    "## 特殊的类型和校验"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8728cd8a-f0aa-45ae-b25e-259990e28cc2",
   "metadata": {},
   "source": [
    "除了普通的单一值类型（如 str、int、float 等）外，你还可以使用从 str 继承的更复杂的单一值类型。\n",
    "\n",
    "例如，在 Image 模型中我们有一个 url 字段，我们可以把它声明为 Pydantic 的 HttpUrl，而不是 str：\n",
    "\n",
    "该字符串将被检查是否为有效的 URL，并在 JSON Schema / OpenAPI 文档中进行记录。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee68ce80-347b-4c30-bcf4-766dfac60941",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydantic import BaseModel, HttpUrl\n",
    "class Image(BaseModel):\n",
    "    url: HttpUrl\n",
    "    name: str"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6952da81-960f-4a78-8810-aaa84e396215",
   "metadata": {},
   "source": [
    "## 带有一组子模型的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13bc3f8c-550e-4978-986c-a3a22611314a",
   "metadata": {},
   "outputs": [],
   "source": [
    "你还可以将 Pydantic 模型用作 list、set 等的子类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c757e082-d480-490a-8ffd-9150cf23ea9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [10840]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:58476 - \"POST /items/5 HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [10840]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from fastapi import FastAPI\n",
    "from pydantic import BaseModel, HttpUrl\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "class Image(BaseModel):\n",
    "    url: HttpUrl\n",
    "    name: str\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: str | None = None\n",
    "    price: float\n",
    "    tax: float | None = None\n",
    "    images: list[Image] | None = None\n",
    "\n",
    "\n",
    "@app.post(\"/items/{item_id}\")\n",
    "async def update_item(item_id: int, item: Item):\n",
    "    results = {\"item_id\": item_id, \"item\": item}\n",
    "    return results\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4310d4c9-9a9b-40d2-ac0f-f1ebdd8f87fb",
   "metadata": {},
   "source": [
    "这将期望（转换，校验，记录文档等）下面这样的 JSON 请求体：\n",
    "{\n",
    "    \"name\": \"Foo\",\n",
    "    \"description\": \"The pretender\",\n",
    "    \"price\": 42.0,\n",
    "    \"tax\": \"\n",
    "    ],\n",
    "    \"images\": [\n",
    "        {\n",
    "            \"url\": \"http://example.com/baz.jpg\",\n",
    "            \"name\": \"The Foo live\"\n",
    "        },\n",
    "        {\n",
    "            \"url\": \"http://example.com/dave.jpg\",\n",
    "            \"name\": \"The Baz\"\n",
    "        }\n",
    "    ]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be421cef-71e2-48b0-9d27-67974b7e0e50",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/items/5' \n",
    "data = {\n",
    "    \"name\": \"Foo\",\n",
    "    \"description\": \"The pretender\",\n",
    "    \"price\": 42.0,\n",
    "    \"tax\": 3.2,\n",
    "    \"images\": [\n",
    "        {\n",
    "            \"url\": \"http://example.com/baz.jpg\",\n",
    "            \"name\": \"The Foo live\"\n",
    "        },\n",
    "        {\n",
    "            \"url\": \"http://example.com/dave.jpg\",\n",
    "            \"name\": \"The Baz\"\n",
    "        }\n",
    "    ]\n",
    "}\n",
    "res = requests.post(url, json=data) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 \n",
    "# '{\"item_id\":5,\"item\":{\"name\":\"Foo\",\"description\":\"The '\n",
    "#  'pretender\",\"price\":42.0,\"tax\":3.2,\"images\":[{\"url\":\"http://example.com/baz.jpg\",\"name\":\"The '\n",
    "#  'Foo live\"},{\"url\":\"http://example.com/dave.jpg\",\"name\":\"The Baz\"}]}}'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4848709-12b1-4cbf-b972-ec560b66faaf",
   "metadata": {},
   "source": [
    "## 深度嵌套模型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddea8a4d-4e1a-4d36-b88d-247b3f80ff8d",
   "metadata": {},
   "source": [
    "你可以定义任意深度的嵌套模型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "95e76d6e-8c38-4e59-80bf-16994e9bd9d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [23660]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:60226 - \"POST /offers/ HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [23660]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from fastapi import FastAPI\n",
    "from pydantic import BaseModel, HttpUrl\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "class Image(BaseModel):\n",
    "    url: HttpUrl\n",
    "    name: str\n",
    "\n",
    "\n",
    "class Item(BaseModel):\n",
    "    name: str\n",
    "    description: str | None = None\n",
    "    price: float\n",
    "    tax: float | None = None\n",
    "    images: list[Image] | None = None\n",
    "\n",
    "class Offer(BaseModel):\n",
    "    name: str\n",
    "    description: str | None = None\n",
    "    price: float\n",
    "    items: list[Item]\n",
    "\n",
    "\n",
    "@app.post(\"/offers/\")\n",
    "async def create_offer(offer: Offer):\n",
    "    return offer\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d73f34db-4160-48ed-b331-36c151e35d54",
   "metadata": {},
   "outputs": [],
   "source": [
    "这将期望（转换，校验，记录文档等）下面这样的 JSON 请求体：\n",
    "{\n",
    "    \"name\": \"FooOffer\",\n",
    "    \"description\": \"The pretender Offer\",\n",
    "    \"price\": 421.0,\n",
    "    \"items\": [\n",
    "        {\n",
    "            \"name\": \"Foo1\",\n",
    "            \"description\": \"The pretender1\",\n",
    "            \"price\": 42.0,\n",
    "            \"tax\": 3.2,\n",
    "            \"images\": [\n",
    "                {\n",
    "                    \"url\": \"http://example.com/baz1.jpg\",\n",
    "                    \"name\": \"The Foo live1\"\n",
    "                },\n",
    "                {\n",
    "                    \"url\": \"http://example.com/dave1.jpg\",\n",
    "                    \"name\": \"The Baz1\"\n",
    "                }\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"name\": \"Foo2\",\n",
    "            \"description\": \"The pretender2\",\n",
    "            \"price\": 43.0,\n",
    "            \"tax\": 3.3,\n",
    "            \"images\": [\n",
    "                {\n",
    "                    \"url\": \"http://example.com/baz2.jpg\",\n",
    "                    \"name\": \"The Foo live2\"\n",
    "                },\n",
    "                {\n",
    "                    \"url\": \"http://example.com/dave2.jpg\",\n",
    "                    \"name\": \"The Baz2\"\n",
    "                }\n",
    "            ]\n",
    "        }\n",
    "    ]\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48ac08d0-1005-4dd3-a853-c870a3f72910",
   "metadata": {},
   "source": [
    "## requests库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2519cf27-5444-4344-8a33-9f8375157d85",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/offers/' \n",
    "data = {\n",
    "    \"name\": \"FooOffer\",\n",
    "    \"description\": \"The pretender Offer\",\n",
    "    \"price\": 421.0,\n",
    "    \"items\": [\n",
    "        {\n",
    "            \"name\": \"Foo1\",\n",
    "            \"description\": \"The pretender1\",\n",
    "            \"price\": 42.0,\n",
    "            \"tax\": 3.2,\n",
    "            \"images\": [\n",
    "                {\n",
    "                    \"url\": \"http://example.com/baz1.jpg\",\n",
    "                    \"name\": \"The Foo live1\"\n",
    "                },\n",
    "                {\n",
    "                    \"url\": \"http://example.com/dave1.jpg\",\n",
    "                    \"name\": \"The Baz1\"\n",
    "                }\n",
    "            ]\n",
    "        },\n",
    "        {\n",
    "            \"name\": \"Foo2\",\n",
    "            \"description\": \"The pretender2\",\n",
    "            \"price\": 43.0,\n",
    "            \"tax\": 3.3,\n",
    "            \"images\": [\n",
    "                {\n",
    "                    \"url\": \"http://example.com/baz2.jpg\",\n",
    "                    \"name\": \"The Foo live2\"\n",
    "                },\n",
    "                {\n",
    "                    \"url\": \"http://example.com/dave2.jpg\",\n",
    "                    \"name\": \"The Baz2\"\n",
    "                }\n",
    "            ]\n",
    "        }\n",
    "    ]\n",
    "}\n",
    "res = requests.post(url, json=data) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 \n",
    "\n",
    "# '{\"name\":\"FooOffer\",\"description\":\"The pretender Offer\",\"price\":421.0,\"items\":[{\"name\":\"Foo1\",\"description\":\"The pretender1\",\"price\":42.0,\"tax\":3.2,\"images\":[{\"url\":\"http://example.com/baz1.jpg\",\"name\":\"The Foo live1\"},{\"url\":\"http://example.com/dave1.jpg\",\"name\":\"The Baz1\"}]},{\"name\":\"Foo2\",\"description\":\"The pretender2\",\"price\":43.0,\"tax\":3.3,\"images\":[{\"url\":\"http://example.com/baz2.jpg\",\"name\":\"The Foo live2\"},{\"url\":\"http://example.com/dave2.jpg\",\"name\":\"The Baz2\"}]}]}'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be34bb96-927a-4233-a3c0-7da3d5b42728",
   "metadata": {},
   "source": [
    "## 纯列表请求体"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1274eae-8a2f-4367-9289-0c8b2ef7bdac",
   "metadata": {},
   "source": [
    "如果你期望的 JSON 请求体的最外层是一个 JSON array（即 Python list），\n",
    "\n",
    "则可以在路径操作函数的参数中声明此类型，就像声明 Pydantic 模型一样：\n",
    "\n",
    "images: List[Image]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c818af51-fcba-4424-afcd-1b55e1f716f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [23660]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:60298 - \"POST /images/multiple/ HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [23660]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from fastapi import FastAPI\n",
    "from pydantic import BaseModel, HttpUrl\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "class Image(BaseModel):\n",
    "    url: HttpUrl\n",
    "    name: str\n",
    "\n",
    "\n",
    "@app.post(\"/images/multiple/\")\n",
    "async def create_multiple_images(images: list[Image]):\n",
    "    return images\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6edf746-3149-4745-bec8-5a303750a227",
   "metadata": {},
   "source": [
    "这将期望（转换，校验，记录文档等）下面这样的 JSON 请求体：\n",
    "[\n",
    "    {\n",
    "        \"url\": \"http://example.com/baz.jpg\",\n",
    "        \"name\": \"The Foo live\"\n",
    "    },\n",
    "    {\n",
    "        \"url\": \"http://example.com/dave.jpg\",\n",
    "        \"name\": \"The Baz\"\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6250000-138a-4a78-983e-3d13ba03795f",
   "metadata": {},
   "source": [
    "## requests库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42c75582-41fd-4b02-8c82-1696b0e34381",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/images/multiple/' \n",
    "data = [\n",
    "    {\n",
    "        \"url\": \"http://example.com/baz.jpg\",\n",
    "        \"name\": \"The Foo live\"\n",
    "    },\n",
    "    {\n",
    "        \"url\": \"http://example.com/dave.jpg\",\n",
    "        \"name\": \"The Baz\"\n",
    "    }\n",
    "]\n",
    "res = requests.post(url, json=data) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 \n",
    "# '[{\"url\":\"http://example.com/baz.jpg\",\"name\":\"The Foo live\"},{\"url\":\"http://example.com/dave.jpg\",\"name\":\"The Baz\"}]'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d928f4ef-dc28-4381-a2d7-cf6a74488fae",
   "metadata": {},
   "source": [
    "## 任意 dict 构成的请求体"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64e737b2-def8-4036-b871-567a271339c2",
   "metadata": {},
   "source": [
    "你也可以将请求体声明为使用某类型的键和其他类型值的 dict。\n",
    "\n",
    "无需事先知道有效的字段/属性（在使用 Pydantic 模型的场景）名称是什么。\n",
    "\n",
    "如果你想接收一些尚且未知的键，这将很有用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "aceaef92-fd96-4079-a47e-2622a36269c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [23660]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:60372 - \"POST /index-weights/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:60389 - \"POST /index-weights/ HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [23660]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from fastapi import FastAPI\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "\n",
    "@app.post(\"/index-weights/\")\n",
    "async def create_index_weights(weights: dict):\n",
    "    return weights\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10063ef3-8a4e-4b56-a64c-50982834cd16",
   "metadata": {},
   "source": [
    "在这种情况下，只要传进来的数据最外层是个dict类型的数据即可。不用管里面的内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05f64b1f-7840-47f1-9c5d-8f0af16311e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/index-weights/' \n",
    "data = {\"item_id\": 5, \"name\": \"张三\"}\n",
    "res = requests.post(url, json=data) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 '{\"item_id\":5,\"name\":\"张三\"}'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad45d658-c4cc-45bc-97cd-a54ac9d18089",
   "metadata": {},
   "source": [
    "字典里面嵌套列表也没问题："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e421f88-4409-4126-8651-4bb54a9109ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/index-weights/' \n",
    "data = {\n",
    "    \"name\": \"Foo\",\n",
    "    \"description\": \"The pretender\",\n",
    "    \"price\": 42.0,\n",
    "    \"tax\": 3.2,\n",
    "    \"images\": [\n",
    "        {\n",
    "            \"url\": \"http://example.com/baz.jpg\",\n",
    "            \"name\": \"The Foo live\"\n",
    "        },\n",
    "        {\n",
    "            \"url\": \"http://example.com/dave.jpg\",\n",
    "            \"name\": \"The Baz\"\n",
    "        }\n",
    "    ]\n",
    "}\n",
    "res = requests.post(url, json=data) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到\n",
    "# '{\"name\":\"Foo\",\"description\":\"The pretender\",\"price\":42.0,\"tax\":3.2,\"images\":[{\"url\":\"http://example.com/baz.jpg\",\"name\":\"The Foo live\"},{\"url\":\"http://example.com/dave.jpg\",\"name\":\"The Baz\"}]}'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
